﻿'use strict';
/*userConcernRouteCtrl  Controllers */
//关注路线
app.controller('userConcernRouteCtrl', ['$scope','$http', "$state",'dataService', '$location', '$ngBootbox',
    function ($scope, $http, $state, dataService, $location, $ngBootbox) {
        var userId = $scope.userId = window.localStorage.id;
        var carCodeStorage = (window.localStorage.carCode == "null") ? " " : window.localStorage.carCode;
        dataService.GetUserBasicInfo($scope.userId).then(function(res){
            $scope.carCode = res.data.info.carNumber ? res.data.info.carNumber:carCodeStorage;
        })
        var skip = $scope.skip = 0;
        var count = $scope.count = 10;
        
        var routeList = $scope.routeList = {
            data: [],
            enableSorting: false,
            //paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                            { displayName: '序号',  field: '', name: 'index',enableColumnMenu: false, cellTooltip: true, headerTooltip: true, cellTemplate: '<div>{{rowRenderIndex + 1}}</div>'},
                            { displayName: "起始地", field: 'startingPlaceName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            { displayName: "目的地", field: 'destinationName', minWidth: 300, enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            { displayName: "生成日期", field: 'operationTime',type:'date',cellFilter: 'date:"yyyy-MM-dd HH:mm:ss"', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            {
                                displayName: "操作",
                                field: '操作',
                                cellFilter: 'checkmark',
                                cellTemplate: ' <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.routeDel(row.entity.id)"></a>', enableColumnMenu: false
                            }],
        };

        //初始化列表
        var getDataList = function (userId) {
            dataService.GetUserFocusLines(userId)
            //$http.get(Mustache.render("/api/" + "Line/GetUserFocusLines?userId={{userId}}", { userId: userId }))
            .then(function (res) {
                $scope.routeList.data = res.data.lines;
                $scope.total = res.data.lines.length;
                $scope.seeks = false;
            }, function (rej) { $scope.seeks = false; });
        };

        getDataList(userId);
        $scope.seeks = true;

        //加载地址下拉
        dataService.GetProvince()
        //$http.get(Mustache.render("/api/" + "Common/GetProvince"))
        .then(function (res) {
            res = res.data;
            $scope.starProvinceList = res.list;
            $scope.endProvinceList = res.list;
        }, function (rej) {
        });

        //删除
        $scope.routeDel = function (id) {
            $ngBootbox.confirm('确认删除此线路？').then(function () {
                dataService.DeleteFocusLine(id)
                //$http.get(Mustache.render("/api/" + "Line/DeleteFocusLine?lineId={{lineId}}", { lineId: id }))
                .then(function (res) {
                    getDataList(userId);
                }, function (rej) { });
            }, function () { return; });
        };


        $scope.starProvince = "";
        $scope.starProvinceList = "";

        $scope.starCity = "";
        $scope.starCityList = "";


        $scope.endProvince = "";
        $scope.endProvinceList = "";

        $scope.endCity = "";
        $scope.endCityList = "";


        $scope.changeStarProvince = function () {
            $scope.starCity = "";
            if ($scope.starProvince === null || $scope.starProvince == "" || $scope.starProvince == undefined) {
                $scope.starProvince = -1;
            }
            dataService.GetCity($scope.starProvince)
            //$http.get(Mustache.render("/api/" + "Common/GetCity?provincecode={{provincecode}}", { provincecode: $scope.starProvince }))
            .then(function (res) {
                res = res.data;
                $scope.starCityList = res.list;
                $scope.areaList = "";
            }, function (rej) {
                console.error("areaApiService.provincecode error: ", rej);
            });
        };
        $scope.changeEndProvince = function () {
            $scope.endCity = "";
            if ($scope.endProvince === null || $scope.endProvince == "" || $scope.endProvince == undefined) {
                $scope.endProvince = -1;
            }
            dataService.GetCity($scope.endProvince)
            //$http.get(Mustache.render("/api/" + "Common/GetCity?provincecode={{provincecode}}", { provincecode: $scope.endProvince }))
            .then(function (res) {
                res = res.data;
                $scope.endCityList = res.list;
            }, function (rej) {
                console.error("areaApiService.provincecode error: ", rej);
            });
        };

        //添加
        $scope.routeAdd = function () {
            $ngBootbox.confirm('确定保存该路线？').then(function () {
                if ($scope.total >= 10) {
                    $ngBootbox.alert('最多添加10条路线');
                    return;
                } else {
                dataService.AddUserFocusLine($scope.userId, $scope.starCity, $scope.endCity)
                //$http.get(Mustache.render("/api/" + "Line/AddUserFocusLine?userId={{userId}}&startingPlaceCode={{startingPlaceCode}}&destinationCode={{destinationCode}}",
                //{
                //    userId: $scope.userId,
                //    startingPlaceCode: $scope.starCity,
                //    destinationCode: $scope.endCity
                //}))
                .then(function (res) {
                    if (res.data.message) {
                        if (res.data.message.indexOf("CommonTransportLinkages Exist")>=0) {
                            $ngBootbox.alert('不能添加相同路线！');
                        }
                    }
                    getDataList(userId);
                }, function (rej) { });
                }
            }, function () { return });

        }
    }]);



